package com.ipay.service;

import com.alibaba.fastjson.JSON;
import com.ipay.bean.Order;
import com.ipay.bean.User;
import com.ipay.mapper.OrderMapper;
import com.ipay.mapper.UserMapper;
import com.ipay.rpc.OrderPayAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author ：前世小和尚（xiaopingzhang8@creditease.cn）
 * @Description ：
 * @ClassName ：OrderPayImpl
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2018/4/5 10:11
 */
@Service("orderPayAPI")
public class OrderPayImpl implements OrderPayAPI {

    private static final Logger log = LoggerFactory.getLogger(OrderPayImpl.class);

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ThreadPoolTaskExecutor executorService;

    @Override
    public String submitOrder(Order order) {
        log.info("订单信息-" + JSON.toJSONString(order));
        int count = orderMapper.insertOrder(order);
        return JSON.toJSONString(order);
    }

    @Override
    public String submitUser(User user) {
        log.info("用户信息-" + JSON.toJSONString(user));
        int count = userMapper.insertUser(user);
        return JSON.toJSONString(user);
    }

    @Override
    public String submitOrderUser(Order order, User user) {
        try {
            MDC.put("tradeId", "123");
            log.info("订单信息-" + JSON.toJSONString(order));
            log.info("用户信息-" + JSON.toJSONString(user));
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        saveInfo(order, user);
                    } catch (Exception e) {

                    }
                }
            });

            return "success";
        } finally {
            MDC.remove("tradeId");
        }
    }

    @Transactional
    void saveInfo(Order order, User user) throws Exception {
        log.info("save data");
        try {
            orderMapper.insertOrder(order);
            userMapper.insertUser(user);
        } catch (Exception e) {
            log.info("save order and user is failed", e);
            throw e;
        }
    }

    @Override
    public void operateRedis() {
        log.info("redis set start");
        boolean flag = stringRedisTemplate.opsForValue().setIfAbsent("name", "zhangsan");
        if (flag) {
            log.info("first redis 设置成功");
        } else {
            log.info("first redis 设置失败");
        }
        boolean flag2 = stringRedisTemplate.opsForValue().setIfAbsent("name", "zhangsan");
        if (flag2) {
            log.info("second redis 设置成功");
        } else {
            log.info("second redis 设置失败");
        }
    }

    @Override
    public Order findOrder(String orderId) {
        return orderMapper.findOrder(orderId);
    }
}
